﻿using Devonline.CloudService.Aliyun;
using IdentityServer4;
using Microsoft.AspNetCore.Authentication.OAuth;

namespace Devonline.Identity.Admin;

/// <summary>
/// 认证配置项
/// </summary>
public class AdminSetting : AppSetting
{
    /// <summary>
    /// 微信认证配置
    /// </summary>
    public AuthOptions Weixin { get; set; }
    /// <summary>
    /// Github 认证配置
    /// </summary>
    public AuthOptions GitHub { get; set; }
    /// <summary>
    /// Gitee 认证配置
    /// </summary>
    public AuthOptions Gitee { get; set; }

    /// <summary>
    /// DataProtection
    /// </summary>
    public DataProtectionOptions DataProtection { get; set; }

    /// <summary>
    /// 短信账户配置
    /// </summary>
    public SmsEndpoint Sms { get; set; }
    /// <summary>
    /// 短信验证码模板配置
    /// </summary>
    public SmsModel SmsCaptcha { get; set; }
    /// <summary>
    /// 短信验证码过期时间, 单位: 分钟
    /// </summary>
    public int CaptchaExpireTime { get; set; } = 5;
}

/// <summary>
/// auth options
/// </summary>
public class AuthOptions
{
    public AuthOptions()
    {
        SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
    }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enable { get; set; }
    /// <summary>
    /// 图标
    /// </summary>
    public string Icon { get; set; }
    //
    // 摘要:
    //     Gets or sets the provider-assigned client id.
    public string ClientId { get; set; }
    //
    // 摘要:
    //     Gets or sets the provider-assigned client secret.
    public string ClientSecret { get; set; }
    //
    // 摘要:
    //     Gets the list of permissions to request.
    public string[] Scope { get; set; }

#nullable enable
    //
    // 摘要:
    //     Gets or sets the authentication scheme corresponding to the middleware responsible
    //     of persisting user's identity after a successful authentication. This value typically
    //     corresponds to a cookie middleware registered in the Startup class. When omitted,
    //     Microsoft.AspNetCore.Authentication.AuthenticationOptions.DefaultSignInScheme
    //     is used as a fallback value.
    public string? SignInScheme { get; set; }
#nullable disable

    /// <summary>
    /// 自配置方法
    /// </summary>
    /// <param name="options"></param>
    public void Configure(OAuthOptions options)
    {
        options.ClientId = ClientId;
        options.ClientSecret = ClientSecret;
        options.SignInScheme ??= SignInScheme;
        if (Scope.IsNotNullOrEmpty())
        {
            options.Scope.AddRange(Scope);
        }
    }
}